<template>
    <div class="goodsinfo-container bk-eee ofh">

        <transition
            @before-enter="beforeEnter"
            @enter="enter"
            @after-enter="afterEnter">
            <div class="ball" v-show="ballFlag" ref="ball"></div>
        </transition>

        <!--轮播图区域-->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <swiper :lunbotuList="lunbotu" :isfull="false"></swiper>
                </div>
            </div>
        </div>

        <!--商品购买区域-->
        <div class="mui-card">
            <div class="mui-card-header">{{ goodsinfo.title }}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <div class="mt10">市场价: ￥{{ goodsinfo.market_price }} &nbsp;&nbsp; 销售价: <span class="f16 fc-red fb">￥{{ goodsinfo.sell_price }}</span></div>
                    <div class="mt10 mb10">购买数量: <numbox @buyNo="getSelectedCount" :max="goodsinfo.stock_quantity"></numbox></div>
                    <mt-button type="primary" size="small">立即购买</mt-button>
                    <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>

                    <!--加入购物车涉及到子组件向父组件传值的问题，子组件向父组件传值实质上就是（事件调用机制）-->
                    <!--事件调用的本质：父向子传递方法，子调用这个方法，同时把数据作为参数传递给这个方法-->
                </div>
            </div>
        </div>

        <!--商品详情区域-->
        <div class="mui-card">
            <div class="mui-card-header">商品详情</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品货号：{{ goodsinfo.goods_no }}</p>
                    <p>库存情况：{{ goodsinfo.stock_quantity }}</p>
                    <p>上架时间：{{ goodsinfo.add_time | dataFormat }}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
            </div>
        </div>
    </div>
</template>

<script>

    import swiper from "../../components/swiper/swiper"
    import numbox from "../../components/numbox/goodsInfo_numbox"

    export default {
        name: "GoodsInfo",
        data(){
            return {
                id: this.$route.params.id,
                lunbotu: [],
                goodsinfo: {},
                ballFlag: false,
                selectedCount:1
            }
        },
        created() {
            this.getLunbotu();
            this.getGoodInfo();
        },
        methods:{
            getLunbotu(){
                this.$axios.get('/api/getthumimages/'+ this.id).then(res => {
                    if(res.data.status === 0){

                        // 为返回值添加一个item.img属性，应为swiper组件中只认识item.img,不认识item.src
                        res.data.message.forEach(item=>{
                           item.img = item.src;
                        });

                        this.lunbotu = res.data.message;
                    }
                });
            },
            getGoodInfo(){
                this.$axios.get('/api/goods/getinfo/'+ this.id).then(res => {
                    if(res.data.status === 0){
                        this.goodsinfo = res.data.message[0];
                    }
                });
            },
            addToShopCar(){
                this.ballFlag = !this.ballFlag;

                // { id: 商品的id，count：要购买的数量，price：商品单价，selected：false}
                // 要拼接出一个保存到store 中 car数组里的商品信息对象
                var goodsinfo = {
                    id: this.id,
                    count: this.selectedCount,
                    price: this.goodsinfo.sell_price,
                    selected: true
                }
                // 调用 store 中的 mutations 来将商品加入购物车
                this.$store.commit('addToCar',goodsinfo)
            },
            goDesc(id){
                this.$router.push({ name: 'goodsdesc', params: { id }})
            },
            goComment(id){
                this.$router.push({ name: 'goodscomment', params: { id }})
            },
            beforeEnter(el){
                el.style.transform = "translate(0, 0)";
            },
            enter(el,done){
                el.offsetWidth;

                // 获取小球距离视窗的对象
                const ballPosition = this.$refs.ball.getBoundingClientRect();
                // 获取徽标距离视窗的对象
                const badgePoisition = document.getElementById("J-badge").getBoundingClientRect();
                // 计算小球到徽标的距离
                const xLocation = badgePoisition.left - ballPosition.left;
                const yLocation = badgePoisition.top - ballPosition.top;

                el.style.transform = `translate(${xLocation}px, ${yLocation}px)`;
                el.style.transition = "all 1s cubic-bezier(.4, -0.3,1,.68)";
                done();
            },
            afterEnter(el){
                this.ballFlag = !this.ballFlag;
            },
            getSelectedCount(count){
                this.selectedCount = count;
                console.log('父组件拿到的数量值为：'+ this.selectedCount)
            }
        },
        components: {
            swiper, numbox
        }
    }
</script>

<style lang="scss" scoped>
    .mui-card-footer {
        display: block;
        button {
            margin-top: 15px;
        }
    }
    .ball {
        position: absolute;
        top: 370px;
        left: 141px;
        width: 15px;
        height: 15px;
        border-radius: 50%;
        background: #f00;
        z-index: 99;
    }
</style>
